﻿// <copyright file="King.cs" company="ALCPU">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Arthur Liberman</author>
// <email>Arthur_Liberman@hotmail.com</email>
// <date>04-07-2010</date>
// <summary>Contains Checkers King definition.</summary>

namespace Chess.Checkers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Media;
    using Chess.Figures;
    using ChessDefinitions;

    /// <summary>
    /// Representative of the King piece
    /// </summary>
    public class King : Man
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="King"/> class.
        /// </summary>
        /// <param name="color">The piece color.</param>
        /// <param name="gameboard">The gameboard.</param>
        /// <param name="position">The starting position.</param>
        public King(IPieceColor color, Gameboard gameboard, Position position)
            : base(color, gameboard, position)
        {
            this.SetImage(color);
            this.Gameboard.MovePiece(position, this);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="King"/> class.
        /// </summary>
        /// <param name="piece">The piece.</param>
        public King(IPiece piece)
            : base(piece)
        {
            this.SetImage(this.Color);
            this.Gameboard.MovePiece(this.Position, this);
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="King"/> class from being created.
        /// </summary>
        private King()
            : base(0, null, new Position())
        {
        }

        /// <summary>
        /// Fills the move options.
        /// </summary>
        /// <returns>List of moves.</returns>
        protected override List<Position> GetMoveOptions()
        {
            List<Position> posList = new List<Position>();

            if (this.Gameboard.IsSquareFree(this.Position + new Position(1, -1)))
            {
                posList.Add(this.Position + new Position(1, -1));
            }

            if (this.Gameboard.IsSquareFree(this.Position + new Position(-1, -1)))
            {
                posList.Add(this.Position + new Position(-1, -1));
            }

            if (this.Gameboard.IsSquareFree(this.Position + new Position(1, 1)))
            {
                posList.Add(this.Position + new Position(1, 1));
            }

            if (this.Gameboard.IsSquareFree(this.Position + new Position(-1, 1)))
            {
                posList.Add(this.Position + new Position(-1, 1));
            }

            return posList;
        }

        /// <summary>
        /// Fills the attack list.
        /// </summary>
        /// <returns>List of possible attacks.</returns>
        protected override List<ChessDefinitions.Position> GetAttackList()
        {
            List<Position> posList = new List<Position>();

            if (this.IsSquareAttackable(CheckersPieceDirection.RightUp))
            {
                posList.Add(this.Position + new Position(1, -1));
            }

            if (this.IsSquareAttackable(CheckersPieceDirection.LeftUp))
            {
                posList.Add(this.Position + new Position(-1, -1));
            }

            if (this.IsSquareAttackable(CheckersPieceDirection.RightDown))
            {
                posList.Add(this.Position + new Position(1, 1));
            }

            if (this.IsSquareAttackable(CheckersPieceDirection.LeftDown))
            {
                posList.Add(this.Position + new Position(-1, 1));
            }

            return posList;
        }

        /// <summary>
        /// Sets the image.
        /// </summary>
        /// <param name="color">The color.</param>
        private void SetImage(IPieceColor color)
        {
            if (color == IPieceColor.Black)
            {
                this.Image = (Brush)App.Current.FindResource("BlackDraughtsKing");
            }
            else if (color == IPieceColor.White)
            {
                this.Image = (Brush)App.Current.FindResource("WhiteDraughtsKing");
            }
        }
    }
}
